package com.lu.manage.modular.system.controller;

import com.lu.manage.core.base.controller.BaseController;
import com.lu.manage.core.common.constant.SystemConstant;
import com.lu.manage.core.common.node.LayuiTreeNode;
import com.lu.manage.core.common.node.ZTreeNode;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.model.Dept;
import com.lu.manage.modular.system.service.DeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program LuBoot
 * @description: 部门控制器
 * @author: zhanglu
 * @create: 2019-11-05 20:13:00
 */
@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    private static String PREFIX = "/modular/system/dept";

    @Autowired
    private DeptService deptService;


/*******************************跳转页面相关*******************************/

    /**
     * 首页
     * @return
     */
    @GetMapping("")
    public Object index(){
        return PREFIX + "/dept.html";
    }

    /**
     * 新增
     * @return
     */
    @GetMapping("/toAdd")
    public Object toAdd(){
        return PREFIX + "/dept_add.html";
    }

    /**
     * 修改
     * @return
     */
    @GetMapping("/toUpdate")
    public Object toUpdate(String id, Model model){
        Dept dept = deptService.getById(id);
        if(ToolUtil.isNotEmpty(dept)){
            Dept pdept = deptService.getById(dept.getPid());
            dept.setPsimpleName(ToolUtil.isNotEmpty(pdept) ? pdept.getSimpleName() : SystemConstant.pid);
        }
        model.addAttribute("obj", dept);
        return PREFIX + "/dept_update.html";
    }


/*******************************操作按钮相关*******************************/

    /**
     * 新增部门
     * @param dept
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions(value = "/system/dept/add")
    public Object add(Dept dept) throws Exception{
        this.deptService.add(dept);
        return new SuccessTip();
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions(value = "/system/dept/update")
    public Object update(Dept dept) throws Exception {

        //如果修改了编号，则该部门的子部门也要修改对应的父级(部门层级修改情况)
        this.deptService.update(dept);

        return new SuccessTip();
    }

    /**
     * 删除部门
     * @param deptId
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = "/system/dept/delete")
    public Object delete(String deptId) throws Exception{
        this.deptService.delete(deptId);
        return new SuccessTip();
    }

    /**
     * 部门排序
     * @param nodes
     * @return
     * @throws Exception
     */
    @PostMapping("/sort")
    @ResponseBody
    @RequiresPermissions(value = "/system/dept/sort")
    public Object sort(String nodes) throws Exception{
        this.deptService.sortAllDepts(nodes);
        return new SuccessTip();
    }

/*******************************查询数据相关*******************************/

    /**
     * 部门列表分页
     * @param map 查询参数
     * @return
     */
    @GetMapping("/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> map){
        return deptService.listPage(map);
    }

    /**
     * 部门左侧树layuiTree
     * @return
     */
    @GetMapping("/deptLayuiTree")
    @ResponseBody
    public Object deptLayuiTree(){
        List<LayuiTreeNode> deptLayuiTree = (List<LayuiTreeNode>) deptService.deptLayuiTree();
        return deptLayuiTree;
    }

    /**
     * 部门tree
     * @return
     */
    @GetMapping("/deptTreeList")
    @ResponseBody
    public Object deptTreeList(){
        List<ZTreeNode> deptTreeList = (List<ZTreeNode>) deptService.deptTreeList();
        deptTreeList.add(ZTreeNode.createParent());
        return deptTreeList;
    }

}
